﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Manager.Tools;

namespace Web.Controllers
{
    public class UserController : Controller
    {
        private readonly Manager.BLL.Admin.M_UserBll bll = new Manager.BLL.Admin.M_UserBll();
        //
        // GET: /Account/
        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 登录提交
        /// </summary>
        /// <param name="forms"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(FormCollection forms)
        {
            string username = PageValidate.InputText(forms["tbxUserName"].Trim(), 20);
            string password = PageValidate.InputText(forms["tbxUserPwd"].Trim(), 32);
            Manager.BLL.Admin.M_UserBll bllUser = new Manager.BLL.Admin.M_UserBll();
            Manager.Model.Admin.M_User loginuser = bllUser.ValidateLogin(username, password);
            if (loginuser != null)
            {
                Session["loginuser"] = loginuser;

                return RedirectToAction("Index", "Home");
            }
            else
            {
                ViewBag.Error = "用户登录失败！";

            }
            return View();
        }

        [HttpGet]
        public ActionResult Logout()
        {
            if (Session["loginuser"] != null)
            {
                Session["loginuser"] = null;
            }
            return RedirectToAction("Index","Home");
        }


        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(Manager.Model.Admin.M_User user)
        {
            if (bll.GetRecordCount("IsValid=1 And UserNickName='" + user.UserNickName + "'") > 1)
                ModelState.AddModelError("", "用户名已存在");
            if (bll.GetRecordCount("IsValid=1 And UserEmail='" + user.UserEmail + "'") > 1)
                ModelState.AddModelError("", "邮箱地址已存在");
            if (bll.GetRecordCount("IsValid=1 And UserTel='" + user.UserTel + "'") > 1)
                ModelState.AddModelError("", "联系方式已存在");

            if (ModelState.IsValid)
            {
                user.UserIPAddr = Manager.Tools.IPAddr.GetClientIP();
                user.UserPwd = Manager.Tools.DEncrypt.DESEncrypt.Encrypt(user.UserPwd);
                int r = bll.Add(user);
                if (r > 0)
                    return RedirectToAction("Index","Home");
                else
                    ModelState.AddModelError("", Resources.Global.msg_error);
            }
            return View(user);
        }

        public JsonResult GetProvince()
        {
            Manager.BLL.Admin.M_ProvinceBll bllP=new Manager.BLL.Admin.M_ProvinceBll();
            
            IList<Manager.Model.Admin.M_Province> query = bllP.GetModelList("");
            return Json(query,JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetCity(string id)
        {
            Manager.BLL.Admin.M_CityBll bllC = new Manager.BLL.Admin.M_CityBll();
            IList<Manager.Model.Admin.M_City> query = bllC.GetModelList("ProvinceID="+id);
            return Json(query, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetDistrict(string id)
        {
            Manager.BLL.Admin.M_DistrictBll bllD = new Manager.BLL.Admin.M_DistrictBll();
            IList<Manager.Model.Admin.M_District> query = bllD.GetModelList("CityID="+id);
            return Json(query, JsonRequestBehavior.AllowGet);
        }
    }
}
